//
//  SecondMovieViewController.m
//  DouBan
//
//  Created by dllo on 16/6/24.
//  Copyright © 2016年 zuzhiming. All rights reserved.
//

#import "SecondMovieViewController.h"
#import "MovieCollectionViewCell.h"
#import "MovieModel.h"

@interface SecondMovieViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain) UICollectionView *movieCollectionView;

@property (nonatomic, retain) CATransition *transitions;

@end

@implementation SecondMovieViewController
/**
 *  重用标识符
 */
static NSString *const movieCollectionViewCell = @"movieCollectionViewCell";

- (void)dealloc
{
    [_movieCollectionView release];
    [_modelArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createCollectionView];
    
    [self createRightBarButtonItem];
    
    //初始化
    //CAtransitions 继承于CAAnimation    animation遍历构造器方法属于父类
    self.transitions = [CATransition animation];
    
    //设置动画时长, 设置代理人
    self.transitions.duration = 1.0f;
    //    self.transitions.delegate = self;
    /**
     枚举值
     kCAMediaTimingFunctionLinear
     kCAMediaTimingFunctionEaseIn
     kCAMediaTimingFunctionEaseOut
     kCAMediaTimingFunctionEaseInEaseOut
     kCAMediaTimingFunctionDefault
     */
    //设置切换速度效果
    self.transitions.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    
    /**
     枚举值:
     kCATransitionFade = 1,    // 淡入淡出
     kCATransitionPush,        // 推进效果
     kCATransitionReveal,      // 揭开效果
     kCATransitionMoveIn,      // 慢慢进入并覆盖效果
     */
    //动画切换风格
    self.transitions.type = kCATransitionFade;
    
    //动画切换方向
    /**
     kCATransitionFromRight//右侧
     kCATransitionFromLeft//左侧
     kCATransitionFromTop//顶部
     kCATransitionFromBottom//底部
     */
    self.transitions.subtype = kCATransitionFromBottom;

    
}

- (void)createRightBarButtonItem
{
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_list@2x"] style:UIBarButtonItemStylePlain target:self action:@selector(didClickedRightBarButton:)];
    rightItem.tintColor = [UIColor blackColor];
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (void)didClickedRightBarButton:(UIBarButtonItem *)button
{
 
    [self.navigationController.view.layer addAnimation:_transitions forKey:nil];
    [self.navigationController popViewControllerAnimated:nil];
}


- (void)createCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH - 80) / 3, (SCREEN_HEIGHT - 100) / 3);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 5, 20);
    flowLayout.minimumLineSpacing = 20;
    flowLayout.minimumInteritemSpacing = 10;
    
    self.movieCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 44) collectionViewLayout:flowLayout];
    self.movieCollectionView.backgroundColor = [UIColor whiteColor];
    self.movieCollectionView.delegate = self;
    self.movieCollectionView.dataSource = self;
    
    [self.view addSubview:_movieCollectionView];
    [_movieCollectionView release];
    
    
    [self.movieCollectionView registerClass:[MovieCollectionViewCell class] forCellWithReuseIdentifier:movieCollectionViewCell];
    
    
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.modelArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MovieModel *model = [self.modelArray objectAtIndex:indexPath.item];
    MovieCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:movieCollectionViewCell forIndexPath:indexPath];
    cell.mvMod = model;
    
    return cell;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationItem.hidesBackButton = YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
